package cn.jinjimi.app.main.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.LinkBean;
import cn.jinjimi.app.main.extend.BaseFragment;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;
import cn.jinjimi.app.main.widget.XRecyclerView.XRecyclerView;
import cn.jinjimi.domain.entity.ProductCategory;
import cn.jinjimi.framework.router.RouterHelper;

/**
 * @author zhenhua
 * @date 2018/5/12
 */

public class ProductCountryFragment extends BaseFragment {
    private ProductCategory mProductCategory;
    private XRecyclerView mRecycleview;
    private ProductCountryAdapter mAdapter;

    public ProductCountryFragment() {
        super();
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_product_country;
    }

    @Override
    protected void initial() {
        mRecycleview = findView().findViewById(R.id.recyclerview);
        mRecycleview.setPullRefreshEnabled(false);
        mRecycleview.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mAdapter = new ProductCountryAdapter();
        mRecycleview.setAdapter(mAdapter);
        if (mProductCategory != null) {
            mAdapter.setData(mProductCategory.subs);
        }
    }

    public void setProductCategory(ProductCategory productCategory) {
        mProductCategory = productCategory;
    }

    class BaseViewHolder extends XRecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }
    }

    class ProductCountryAdapter extends XRecyclerView.Adapter<BaseViewHolder> {

        private List<LinkBean> mData = new ArrayList<>();

        public void setData(List<LinkBean> data) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }

        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ProductCountryViewHolder(
                LayoutInflater.from(getActivity()).inflate(R.layout.item_product_country, parent, false)
            );
        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            if (holder instanceof ProductCountryViewHolder) {
                ((ProductCountryViewHolder) holder).render(mData.get(position));
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    class ProductCountryViewHolder extends BaseViewHolder {
        private final TextView mLabel;
        private final FrescoImageView mImage;
        private final View mRoot;

        ProductCountryViewHolder(View v) {
            super(v);
            mLabel = v.findViewById(R.id.label);
            mImage = v.findViewById(R.id.image);
            mRoot = v;
        }

        public void render(final LinkBean link) {
            if (link != null) {
                mImage.loadView(link.img);
                mLabel.setText(link.label);
            }
            mRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RouterHelper.toUrl(getActivity(), link.link);
                }
            });
        }
    }
}
